﻿using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
using System;
using System.Web;

public partial class Site : System.Web.UI.MasterPage {
    //public string ResolveURL()
    //{
    //    return "http://" + Request.Url.Host + ":" + Request.Url.Port + Request.Url.Segments[0].ToString().Replace("//", "/") + Request.Url.Segments[1].ToString().Replace("//", "/");
    //}


    public string ResolveURL(string relativeUrl)
    {
        if (relativeUrl == null) throw new ArgumentNullException("url", "url can not be null");
        if (relativeUrl.Length == 0) throw new ArgumentException("The url can not be an empty string", "url");

        // there is no ~ in the first character position, just return the url 
        if (relativeUrl[0] != '~') return relativeUrl;

        string applicationPath = HttpContext.Current.Request.ApplicationPath;

        // there is just the ~ in the URL, return the applicatonPath 
        if (relativeUrl.Length == 1) return applicationPath;

        // assume url looks like ~somePage 
        int indexOfUrl = 1;

        // determine the middle character 
        string midPath = (applicationPath.Length > 1) ? "/" : string.Empty;

        // if url looks like ~/ or ~\ change the indexOfUrl to 2 
        if (relativeUrl[1] == '/' || relativeUrl[1] == '\\') indexOfUrl = 2;

        return applicationPath + midPath + relativeUrl.Substring(indexOfUrl);
    }// ResolveUrl
    protected void btnLogout_Click(object sender, System.EventArgs e)
    {
        Session["gUserId"] = null;
        Response.Redirect(ResolveURL("~/Login.aspx"));
    }
    public void Page_Load(object sender, System.EventArgs e)
    {
        if (Session["gUserID"] == null)
            Response.Redirect(ResolveURL("~/Login.aspx"));
    }
}
